package com.gzbugu.bugusys.action;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.fields.DatabaseFields;
import com.gzbugu.bugusys.vo.Staff;
import com.gzbugu.bugusys.vo.Travel;
import com.gzbugu.bugusys.vo.Type;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Transactional
public class AddTravelAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String staffId;
	private Date travelStart;
	private Date travelEnd;
	private int travelSubsidy;
	private Integer travelTypeId;
	private Integer travelHandInId;
	private int travelAmountPayable;
	private int travelAmountPaid;
	private Integer travelSubsidyStatus;
	private short typeByStatus;

	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;
	@Resource(name = "databaseFields")
	DatabaseFields databaseFields;

	@Transactional
	public String execute() {
		if (staffId == null) {
			return INPUT;
		}
		Travel instance = new Travel();
		instance.setStaff((Staff) baseDAO.findById(staffId,
				com.gzbugu.bugusys.fields.VoClasseFields.STAFF));
		instance.setNotpaid(travelAmountPayable - travelAmountPaid);
		instance.setPaid(travelAmountPaid);
		instance.setPayable(travelAmountPayable);
		instance.setEnd(travelEnd);
		instance.setTypeByTypeId((Type) baseDAO.findById(travelTypeId.shortValue(),
				com.gzbugu.bugusys.fields.VoClasseFields.TYPE));
		instance.setStart(travelStart);
		instance.setSubsidy(travelSubsidy);
		instance.setTypeByHandInId(travelHandInId == 0 ? databaseFields
				.getTRUEFALSE_FALSE() : databaseFields.getTRUEFALSE_TRUE());
		instance.setTypeByStatus(typeByStatus == 0 ? databaseFields
				.getTRUEFALSE_FALSE() : databaseFields.getTRUEFALSE_TRUE());
		baseDAO.save(instance);
		return SUCCESS;
	}

	public void validate() {
		if (staffId == null) {
			return;
		}
		if (staffId.equals("")) {
			this.addActionError("员工号不能为空");
			return;
		}
		if (travelHandInId == null) {
			travelHandInId = 0;
		}
		if (travelHandInId == null) {
			this.addActionError("请输入上交金额");
		}
		if (travelSubsidyStatus == null) {
			travelSubsidyStatus = 0;
		}
		if (baseDAO.findById(staffId,
				com.gzbugu.bugusys.fields.VoClasseFields.STAFF) == null) {
			this.addActionError("该员工不存在");
		}
		if (travelStart == null || travelEnd == null) {
			this.addActionError("请输入差旅时间");
		} else if (travelStart.after(travelEnd)) {
			this.addActionError("差旅时间有误");
		}
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public Date getTravelStart() {
		return travelStart;
	}

	public void setTravelStart(Date travelStart) {
		this.travelStart = travelStart;
	}

	public Date getTravelEnd() {
		return travelEnd;
	}

	public void setTravelEnd(Date travelEnd) {
		this.travelEnd = travelEnd;
	}

	public int getTravelSubsidy() {
		return travelSubsidy;
	}

	public void setTravelSubsidy(int travelSubsidy) {
		this.travelSubsidy = travelSubsidy;
	}

	public Integer getTravelTypeId() {
		return travelTypeId;
	}

	public void setTravelTypeId(Integer travelTypeId) {
		this.travelTypeId = travelTypeId;
	}

	public Integer getTravelHandInId() {
		return travelHandInId;
	}

	public void setTravelHandInId(Integer travelHandInId) {
		this.travelHandInId = travelHandInId;
	}

	public int getTravelAmountPayable() {
		return travelAmountPayable;
	}

	public void setTravelAmountPayable(int travelAmountPayable) {
		this.travelAmountPayable = travelAmountPayable;
	}

	public int getTravelAmountPaid() {
		return travelAmountPaid;
	}

	public void setTravelAmountPaid(int travelAmountPaid) {
		this.travelAmountPaid = travelAmountPaid;
	}

	public Integer getTravelSubsidyStatus() {
		return travelSubsidyStatus;
	}

	public void setTravelSubsidyStatus(Integer travelSubsidyStatus) {
		this.travelSubsidyStatus = travelSubsidyStatus;
	}

	public short getTypeByStatus() {
		return typeByStatus;
	}

	public void setTypeByStatus(short typeByStatus) {
		this.typeByStatus = typeByStatus;
	}

}
